package com.neusoft.bizcore.auth.repository.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.auth.model.AuthOganization;
import com.neusoft.bizcore.auth.model.QAuthOganization;
import com.neusoft.bizcore.auth.repository.CustomizedAuthOganizationRepository;
import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedAuthOganizationRepositoryImpl extends AbstractRepository
        implements CustomizedAuthOganizationRepository {

    @Override
    public Page<AuthOganization> search(final Pageable pageable, final Searchable searchable) {
        final QAuthOganization qAuthOganization = QAuthOganization.authOganization;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<AuthOganization> query = this.query().selectFrom(qAuthOganization);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return AuthOganization.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QAuthOganization qAuthOganization = QAuthOganization.authOganization;

        where.and(super.containsStr(qAuthOganization.name, searchable, "name"));
        where.and(super.containsStr(qAuthOganization.code, searchable, "code"));
        where.and(super.containsStr(qAuthOganization.parent.code, searchable, "parent"));
        return where;
    }

}
